package com.mall.service.mallGroupbBuy;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.mallGroupBuy.MallTuanCodeService;
import com.mall.custom.dao.read.MallTuanCodeReadMapper;
import com.mall.custom.dao.write.MallTuanCodeWriteMapper;
import com.mall.pojo.MallTuanCode;
import com.mall.pojo.MallTuanCodeExample;
import com.mall.pojo.MallTuanCodeExample.Criteria;
import com.mall.query.Page;
@Service
public class MallTuanCodeServiceImpl implements MallTuanCodeService {
	
	@Autowired
	private MallTuanCodeReadMapper mallTuanCodeReadMapper;

	@Autowired
	private MallTuanCodeWriteMapper mallTuanCodeWriteMapper;

	@Override
	public List<MallTuanCode> getMallTuanCodeListByUserId(Long mallId, Long userId, Integer status) {
		MallTuanCodeExample example = new MallTuanCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andMallIdEqualTo(mallId);
		criteria.andUserIdEqualTo(userId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		return mallTuanCodeReadMapper.selectByExample(example);
	}

	@Override
	public List<MallTuanCode> getMallTuanCodeListByUserIdAndCode(Long mallId, Long userId, String code, Integer status) {
		MallTuanCodeExample example = new MallTuanCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andMallIdEqualTo(mallId);
		criteria.andUserIdEqualTo(userId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		criteria.andCodeEqualTo(code);
		return mallTuanCodeReadMapper.selectByExample(example);
	}

	@Override
	public Long insertMallTuanCodeList(List<MallTuanCode> mallTuanCodeList) {
		return this.mallTuanCodeWriteMapper.betchInsertMallTuanCode(mallTuanCodeList);
//		for (int i = 0; i < mallTuanCodeList.size(); i++) {
//			this.mallTuanCodeWriteMapper.insert(mallTuanCodeList.get(i));
//		}
//		return 1l;
	}

	@Override
	public Integer updateMallTuanCodeByCodeAndUserIdAndMallId(Long id, MallTuanCode mallTuanCode,
			Integer updateStatus) {
		mallTuanCode.setStatus(updateStatus);
		mallTuanCode.setId(id);
		return this.mallTuanCodeWriteMapper.updateByPrimaryKey(mallTuanCode);
	}

	@Override
	public List<MallTuanCode> getMallTuanCodeList(Long userId, Integer status, Page page) {
		MallTuanCodeExample example = new MallTuanCodeExample();
		if (page == null) {
			page = Page.getInitPage(10);
		}
		example.setLimit(page.getLength());
		example.setStart(page.getBegin());
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		if (status != null) {
			criteria.andStatusEqualTo(status);
		}
		return mallTuanCodeReadMapper.selectByExample(example);
	}

	@Override
	public List<MallTuanCode> getMallTuanCodeListByTradeAndUserId(Long userId, Long orderId) {
		MallTuanCodeExample example = new MallTuanCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andStatusNotEqualTo(-1);
		criteria.andTradeIdEqualTo(orderId);
		return mallTuanCodeReadMapper.selectByExample(example);
	}

	@Override
	public int getMallTuanCodeUseCount(Long userId, Long orderId) {
		MallTuanCodeExample example = new MallTuanCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andStatusEqualTo(1);
		criteria.andTradeIdEqualTo(orderId);
		return this.mallTuanCodeReadMapper.countByExample(example);
	}
	

}
